<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="/jwplayer/jwplayer.js" ></script>
<script type="text/javascript">jwplayer.key="ABCDEFGHIJKLMOPQ";</script>
</head>
<body>
<%@ page  import="java.io.FileInputStream" %>
<%@ page  import="java.io.BufferedInputStream"  %>
<%@ page  import="java.io.File"  %>
<%@ page import="java.io.IOException" %>


<%
String pfad;
String filename;
	Object tmpPfad = request.getAttribute("pfad");
	if (tmpPfad != null){
		 pfad = (String) tmpPfad;
		 pfad = pfad.replace("%20", " ");
		 String[] teile = pfad.split("/");
		 int laenge = teile.length;
		 filename = teile[laenge-1];
		 
	} else {
		 pfad="D:\\Pride.m4a";
		 filename = "Pride.m4a";
	}
   // you  can get your base and parent from the database
   String base="/J2E_iTunes/files";
// you can  write http://localhost
   String pfadh="D:\\Pride.m4a";

BufferedInputStream buf=null;
   ServletOutputStream myOut=null;

try{

	myOut = response.getOutputStream();
     File myfile = new File(pfad);
     int dot = pfad.lastIndexOf('.');
		String extension = pfad.substring(dot + 1);
     //set response headers
     if (extension.equalsIgnoreCase("m4a")) {
			response.setHeader("Content-Type", "audio/mp4");
		} else if(extension.equalsIgnoreCase("mp3")){
			response.setHeader("Content-Type", "audio/mpeg");
		} else if(extension.equalsIgnoreCase("mp4")){
			response.setHeader("Content-Type", "video/mpeg");
		}else {
			response.setHeader("Content-Type", "application/octet-stream");
		}
     
     response.addHeader(
        "Content-Disposition","attachment; filename=\""+filename +"\"");

     response.setContentLength( (int) myfile.length( ) );
     
     FileInputStream input = new FileInputStream(myfile);
     buf = new BufferedInputStream(input);
     int readBytes = 0;

     //read from the file; write to the ServletOutputStream
     while((readBytes = buf.read( )) != -1)
       myOut.write(readBytes);

} catch (IOException ioe){
     
        throw new ServletException(ioe.getMessage( ));
         
     } finally {
         
     //close the input/output streams
         if (myOut != null)
             myOut.close( );
          if (buf != null)
          buf.close( );
         
     }

   
   
%>
</body>
</html>